﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using BusinessLayer;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;

namespace DeAn_LapTrinhCSDL.UserControl
{
    public partial class ucTKTinhHinhNhapXuat : DevExpress.XtraEditors.XtraUserControl
    {
        VatTu vt;
        PNhap pn;
        PXuat px;
        public ucTKTinhHinhNhapXuat()
        {
            InitializeComponent();
            vt = new VatTu();
            vt.connect.ConnectionString = Program.strConnect;
            pn = new PNhap();
            pn.connect.ConnectionString = Program.strConnect;
            px = new PXuat();
            px.connect.ConnectionString = Program.strConnect;
        }

        private void ucTKTinhHinhNhapXuat_Load(object sender, EventArgs e)
        {
            date_FromDate.DateTime = DateTime.Now;
            date_ToDate.DateTime = DateTime.Now;
            LoadComBoBoxVattu();
        }
        public void LoadComBoBoxVattu()
        {
            GridView view = new GridView();
            RepositoryItemGridLookUpEdit cboEdit = comboBox_VatTu.Properties;
            cboEdit.View.OptionsView.ShowAutoFilterRow = true;
            cboEdit.View = view;
            cboEdit.DataSource = vt.GetData();
            cboEdit.ValueMember = "MAVTU";
            cboEdit.DisplayMember = "TENVTU";
            cboEdit.TextEditStyle = TextEditStyles.Standard;
            //cboEdit = null;
            cboEdit.View.Columns["MAVTU"].Visible = false;
            cboEdit.View.Columns["TENVTU"].Caption = "Tên vật tư";
            cboEdit.View.Columns["TENVTU"].Width = 100;
            cboEdit.View.Columns["DVTINH"].Caption = "Đơn vị tính";
            cboEdit.View.Columns["PHANTRAM"].Caption = "Phần trăm";
            cboEdit.View.Columns["HINH"].Visible = false;
            comboBox_VatTu.Text = "all";
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            string mavt = string.Empty;
            if (radioButton_PNhap.Checked)
            {
                if (comboBox_VatTu.Text.Equals(string.Empty))
                    mavt = string.Empty;
                else
                    mavt = comboBox_VatTu.Properties.View.GetFocusedDataRow()["MAVTU"].ToString();
                gridView1.GroupPanelText = "Tổng số lượng phiếu nhập";
                gridControl1.DataSource = pn.GetData(date_FromDate.DateTime, date_ToDate.DateTime, mavt);
            }
            else if (radioButton_PXuat.Checked)
            {
                if (comboBox_VatTu.Text.Equals(string.Empty))
                    mavt = string.Empty;
                else
                    mavt = comboBox_VatTu.Properties.View.GetFocusedDataRow()["MAVTU"].ToString();
                gridView1.GroupPanelText = "Tổng số lượng phiếu xuất";
                gridControl1.DataSource = px.GetData(date_FromDate.DateTime, date_ToDate.DateTime, mavt);
            }
        }

        private void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            string mavt = string.Empty;
            if (radioButton_PNhap.Checked)
            {
                if (comboBox_VatTu.Text.Equals(string.Empty))
                    mavt = string.Empty;
                else
                    mavt = comboBox_VatTu.Properties.View.GetFocusedDataRow()["MAVTU"].ToString();
                frmReport report = new frmReport();
                report.reportObject = "TongSLNhap";
                report.data = pn.GetData(date_FromDate.DateTime, date_ToDate.DateTime, mavt);
                report.Show();
            }
            else if (radioButton_PXuat.Checked)
            {
                if (comboBox_VatTu.Text.Equals(string.Empty))
                    mavt = string.Empty;
                else
                    mavt = comboBox_VatTu.Properties.View.GetFocusedDataRow()["MAVTU"].ToString();
                frmReport report = new frmReport();
                report.reportObject = "TongSLXuat";
                report.data = px.GetData(date_FromDate.DateTime, date_ToDate.DateTime, mavt);
                report.Show();
            }
        }
    }
}
